import 'package:flutter/material.dart';
import '../../model/post.dart';

/**
 * Card 小碎片
 */
class CardDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return CardStateDemo();
  }
}

class CardStateDemo extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return CardState();
  }
}

class CardState extends State<CardStateDemo> {
  List<String> _chipsTitleList = ['Apple', 'Banana', 'Lemon'];

  List<String> _actionChipsTitleList = ['Action1', 'Action2', 'Action3'];

  List<String> _filterChipsTitleList = ['Filter1', 'Filter2', 'Filter3'];

  String _actionChipsContent = 'Nothing';

  List<String> _selectedFilterList = [];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('CardDemo'),
        elevation: 0.0,
      ),
      body: Container(
        padding: EdgeInsets.all(8.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            //Wrap类似Android流布局
            Wrap(
              //控件间隔
              spacing: 8.0,
              //行间距
              runSpacing: 8.0,
              children: <Widget>[
                Chip(label: Text('Left')),
                SizedBox(
                  width: 8.0,
                ),
                Chip(
                  label: Text(
                    'Center',
                    style: TextStyle(color: Colors.white),
                  ),
                  backgroundColor: Colors.orange,
                ),
                SizedBox(
                  width: 8.0,
                ),
                Chip(
                  label: Text(
                    'Nihao',
                    style: TextStyle(color: Colors.green),
                  ),
                  avatar: CircleAvatar(
                    backgroundColor: Colors.grey,
                    child: Text(
                      '秀',
                      style: TextStyle(color: Colors.blue),
                    ),
                  ),
                ),
                SizedBox(
                  width: 8.0,
                ),
                Chip(
                  label: Text(
                    '显示头像图片',
                    style: TextStyle(color: Colors.orange),
                  ),
                  avatar: CircleAvatar(
                    backgroundImage: NetworkImage(postList[0].imageUrl),
                  ),
                ),
                Chip(
                  label: Text('delete'),
                  deleteButtonTooltipMessage: 'show delete content',
                  deleteIcon: Icon(Icons.delete),
                  deleteIconColor: Colors.red,
                  onDeleted: () {},
                ),
                //横线
                Divider(
                  color: Colors.grey,
                  height: 32.0,
                ),
                Wrap(
                  spacing: 8.0,
                  children: _chipsTitleList.map((tag) {
                    return Chip(
                      label: Text(tag),
                      onDeleted: () {
                        setState(() {
                          _chipsTitleList.remove(tag);
                          print(
                              "_chipsTitleList.remove($tag): ${_chipsTitleList.toString()}");
                        });
                      },
                    );
                  }).toList(),
                ),
                //分割线
                Divider(
                  color: Colors.grey,
                  //左右缩进
                  indent: 20.0,
                  height: 32.0,
                ),
                Container(
                  width: double.infinity,
                  child: Text('you are choose action is $_actionChipsContent'),
                ),
                Wrap(
                  spacing: 8.0,
                  children: _actionChipsTitleList.map((tag) {
                    return ActionChip(
                      label: Text(tag),
                      onPressed: () {
                        setState(() {
                          _actionChipsContent = tag;
                        });
                      },
                    );
                  }).toList(),
                ),
                Divider(
                  color: Colors.grey,
                  //左右缩进
                  indent: 20.0,
                  height: 32.0,
                ),
                Container(
                  width: double.infinity,
                  child: Text('FilterChips: $_selectedFilterList'),
                ),
                Wrap(
                  spacing: 8.0,
                  children: _filterChipsTitleList.map((tag) {
                    return FilterChip(
                      label: Text(tag),
                      selected: _selectedFilterList.contains(tag),
                      onSelected: (value) {
                        setState(() {
                          if (_selectedFilterList.contains(tag)) {
                            _selectedFilterList.remove(tag);
                          } else {
                            _selectedFilterList.add(tag);
                          }
                        });
                      },
                    );
                  }).toList(),
                ),
              ],
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          setState(() {
            _chipsTitleList = ['Apple', 'Banana', 'Lemon'];
            debugPrint('${_chipsTitleList.toString()}');
            _selectedFilterList = [];
          });
        },
        child: Icon(Icons.restore),
      ),
    );
  }
}
